package seu.evaluation.metric;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import seu.xml.validator.GlobalCons;



/**
 * 
 * @author samantha
 * 架构度量入口，该类职责：让用户选择需要进行度量的属性，将属性列表传递给接下类的度量类
 *
 */
public class MetricMainFrame extends JFrame implements ActionListener,ItemListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static MetricMainFrame frame;
	private JList<String> attributeList;
	private JList<String> chooseList;
	private JButton chooseButton;
	private JButton confirmButton;
	private JButton cancelButton;
	private Container container;
	private final String attributeNames[] = { "Maintainability", "Reliability" };
	private MetricContext metricContext;
	
	private ButtonGroup buttonGroup = new ButtonGroup();	
	private JPanel leftPanel = new JPanel();
	private JPanel rightPanel = new JPanel();
	private JPanel middlePanel = new JPanel();
	private JPanel downPanel = new JPanel();
		
	public static MetricMainFrame getFrame() {
		if (frame == null)
			return new MetricMainFrame();
		else
			return frame;
	}
	
	private MetricMainFrame() {
		this.setTitle("Architecture Metrics");
		this.setSize(400, 200);
		this.setLocation(500, 300);
		
		
		container = getContentPane();
		container.setLayout( new BorderLayout() );
		container.add( leftPanel, BorderLayout.WEST);
		container.add(rightPanel, BorderLayout.EAST);
		container.add(middlePanel, BorderLayout.CENTER);
		container.add(downPanel, BorderLayout.SOUTH);

		
		DefaultListModel<String> attributeListModel = new DefaultListModel<String>();
		for( String singleAttribute: attributeNames )
			attributeListModel.addElement(singleAttribute);
		attributeList = new JList<String>( attributeListModel );
		attributeList.setFixedCellWidth(120);
		attributeList.setFixedCellHeight(20);
		attributeList.setVisibleRowCount(5);	
		leftPanel.add( new JScrollPane(attributeList) );
		
		final DefaultListModel<String> chooseListModel = new DefaultListModel<String>();
		chooseList = new JList<String>( chooseListModel );
		chooseList.setFixedCellHeight(20);
		chooseList.setFixedCellWidth(120);
		chooseList.setVisibleRowCount(5);
		rightPanel.add( new JScrollPane(chooseList) );
			
		
		chooseButton = new JButton( "Choose >>>" ); 
		chooseButton.addActionListener(
				new ActionListener(){
					public void actionPerformed( ActionEvent event){
						chooseListModel.addElement(attributeList.getSelectedValue() );
						//try如果没有选择，添加的是null
					}
				}
		);
		middlePanel.add( chooseButton );
		buttonGroup.add(chooseButton);

		cancelButton = new JButton( "<<< Cancel" );
		cancelButton.addActionListener(
			new ActionListener(){
				public void actionPerformed( ActionEvent event){
					chooseListModel.removeElement( chooseList.getSelectedValue() );				
				}
			}
		);
		middlePanel.add( cancelButton);
		buttonGroup.add(cancelButton);

		
		confirmButton = new JButton( "Confirm" );
		confirmButton.addActionListener(
			new ActionListener(){
				public void actionPerformed( ActionEvent event){
					
					ArrayList<String> chooseArray = new ArrayList<String>();
					for( int i=0; i<chooseListModel.getSize(); i++)
						chooseArray.add( chooseListModel.get(i));		
					metricContext = new MetricContext( chooseArray );	
					MetricResultFrame1.getFrame( metricContext.getTotalResults() );
					
				}
			}
		);	
		downPanel.add( confirmButton );		
	
		this.setVisible(true);		
	}

	public static void main(String args[]){
		GlobalCons.ucDiagramPath.add("UsecaseDiagram.uxf");
		//GlobalCons.ucDiagramPath.add("Use case.uxf");
		GlobalCons.dplmtDiagramPath.add("DeploymentDiagram.uxf");
		GlobalCons.sqcDiagramPath.add("LocalOperationSd.uxf");
		GlobalCons.sqcDiagramPath.add("RemoteReadSd1.uxf");
		GlobalCons.sqcDiagramPath.add("RemoteReadSd2.uxf");
		GlobalCons.sqcDiagramPath.add("RemoteWriteSd.uxf");
		GlobalCons.cmpnDiagramPath.add("Component1.uxf");
		GlobalCons.cmpnDiagram=true;
		GlobalCons.dplmtDiagram=true;
		GlobalCons.sqcDiagram=true;
		GlobalCons.ucDiagram=true;
		MetricMainFrame.getFrame();	
	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

}
